package com.msb.mall.producet.service.impl;

import com.msb.mall.producet.dao.CategoryBrandRelationDao;
import com.msb.mall.producet.entity.CategoryBrandRelationEntity;
import com.msb.mall.producet.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.producet.dao.BrandDao;
import com.msb.mall.producet.entity.BrandEntity;
import com.msb.mall.producet.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        //获取key信息
        String key = (String)params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            /// 添加对应的查询条件
            wrapper.eq("brand_id", key).or().like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 更新品牌信息
     * @param brand 品牌实体
     */
    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        //1.更新原始数据
        this.updateById(brand);
        if (StringUtils.isNotEmpty(brand.getName())) {
            //同步更新级联关系中的数据
            categoryBrandRelationService.updateBrandName(brand.getBrandId(),brand.getName());

            //同步更新其他的品牌名称的冗余数据
        }

    }

}